from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from user.api import getUserIdByCode, getUserById, getUserDetailByTicket
from user.models import User as UserModel


def LoginView(request):
    if request.method == 'GET':
        print("1111111111111111111")
        print("request.GET")
        usercode = request.GET.get('code', default='0')
        state = request.GET.get('state', default='0')
        if usercode == '0':
            return HttpResponseRedirect("/user/msg?status=2")
        else:
            print("33333333333333333333333333333")
            print("获取用户登录CODE成功", usercode)
            idReq = getUserIdByCode(usercode)
            idReq_code = idReq['code']
            if idReq_code == 0:
                print("44444444444444444444444444")
                idReq_data = idReq['data']
                print("获取用户信息成功", idReq_data)
                userid = idReq_data['userid']
                try:
                    user = UserModel.objects.get(wxid=userid)
                    request.session['userid'] = user.id
                    return HttpResponseRedirect("/"+state+"/index")
                except UserModel.DoesNotExist:
                    print("55555555555555555555555")
                    regurl = "https://open.weixin.qq.com/connect/oauth2/authorize?"
                    regurl += "appid=wwcb3168f059eec7d6&redirect_uri="
                    regurl += "https%3A%2F%2Fwecom.qhduhu.com%2Fuser%2Freg"
                    regurl += "&agentid=1000002&response_type=code&scope=snsapi_privateinfo"
                    regurl += "&state=" + state
                    regurl += "#wechat_redirect"
                    print("regurl")
                    print(regurl)
                    return HttpResponseRedirect(regurl)
            else:
                print("6666666666666666666666")
                idReq_msg = idReq['msg']
                print("获取用户信息失败", idReq_msg)
                msgUrl = "/user/msg?status=2"
                msgUrl += "&title=出错了"
                msgUrl += "&msg=获取用户信息失败"
                return HttpResponseRedirect(msgUrl)
    else:
        print("777777777777777777777777")
        msgUrl = "/user/msg?status=2"
        msgUrl += "&title=出错了"
        msgUrl += "&msg=请求方法错误"
        return HttpResponseRedirect(msgUrl)


def RegView(request):
    if request.method == 'GET':
        print("8888888888888888888888RegView")
        print("request.GET")
        usercode = request.GET.get('code', default='0')
        state = request.GET.get('state', default='0')
        if usercode == '0':
            print("获取用户登录CODE失败")
            return HttpResponseRedirect("/user/msg?status=2")
        else:
            print("获取用户登录CODE成功", usercode)
            idReq = getUserIdByCode(usercode)
            idReq_code = idReq['code']
            if idReq_code == 0:
                idReq_data = idReq['data']
                print("获取用户信息成功", idReq_data)
                user_ticket = idReq_data['user_ticket']
                detReq = getUserDetailByTicket(user_ticket)
                print("detReq=================", detReq)
                detReq_code = detReq['code']
                if detReq_code == 0:
                    detReq_data = detReq['data']
                    print("获取用户信息成功", detReq_data)
                    mobile = detReq_data["mobile"]
                    if mobile == "":
                        msgUrl = "/user/msg?status=2"
                        msgUrl += "&title=非法用户"
                        msgUrl += "&msg=您在企业微信中的手机号码为空，无法验证您的身份，请您先完善您的企业微信个人资料后再试"
                        return HttpResponseRedirect(msgUrl)
                    try:
                        user = UserModel.objects.get(mobile=mobile)
                        user.wxid = detReq_data["userid"]
                        user.gender = detReq_data["gender"]
                        user.avatar = detReq_data["avatar"]
                        user.qrcode = detReq_data["qr_code"]
                        user.email = detReq_data["email"]
                        user.save()
                        request.session['userid'] = user.id
                        return HttpResponseRedirect("/"+state+"/index")
                    except UserModel.DoesNotExist:
                        msgUrl = "/user/msg?status=2"
                        msgUrl += "&title=非法用户"
                        msgUrl += "&msg=没有您的手机号码信息，请联系管理员添加"
                        return HttpResponseRedirect(msgUrl)
                    except Exception as e:
                        msgUrl = "/user/msg?status=2"
                        msgUrl += "&title=系统错误"
                        msgUrl += "&msg=系统错误，请联系管理员"
                        return HttpResponseRedirect(msgUrl)
                else:
                    msgUrl = "/user/msg?status=2"
                    msgUrl += "&title=出错了"
                    msgUrl += "&msg=获取用户信息失败"
                    return HttpResponseRedirect(msgUrl)
            else:
                msgUrl = "/user/msg?status=2"
                msgUrl += "&title=出错了"
                msgUrl += "&msg=获取用户信息失败"
                return HttpResponseRedirect(msgUrl)
    else:
        msgUrl = "/user/msg?status=2"
        msgUrl += "&title=出错了"
        msgUrl += "&msg=请求方法错误"
        return HttpResponseRedirect(msgUrl)


def IndexView(request):
    userid = request.session.get('userid', 0)
    if userid == 0:
        return redirect('user:login')
    else:
        user = UserModel.objects.get(id=userid)
    return render(request, "user/index.html", locals())


def MsgView(request):
    # 0 警告 1 成功  2 失败
    status = request.GET.get('status',0)
    title = request.GET.get('title','出错了')
    msg = request.GET.get('msg','')
    desc = request.GET.get('desc','')
    url = request.GET.get('url', 0)
    urltitle = request.GET.get('urltitle', '')
    return render(request, "user/msg.html", locals())
